1   /*
2    * Created on 2004/10/15
3    */
4   package org.apache.velocity.tools.generic.introspection;
5   
6   import java.io.IOException;
7   import java.lang.reflect.Field;
8   import java.lang.reflect.Method;
9   
10  import org.apache.velocity.exception.MethodInvocationException;
11  import org.apache.velocity.exception.ParseErrorException;
12  import org.apache.velocity.exception.ResourceNotFoundException;
13  import org.ieee.shinobu.demo.velocity.AbstractVelocityTestCase;
14  
15  /***
16   * @author shinobu
17   */
18  public class PublicFieldUberspectTest extends AbstractVelocityTestCase {
19  
20      public void testNormalGet() throws Exception {
21          this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
22  
23          this.getContext().put("ctx", this.getContext());
24          this.getContext().put("fields", new Fields());
25  
26          this.setTemplate("" +
27                  "$fields.theField\n" +
28                  "");
29  
30          this.setExpected("" +
31                  "public field\n" +
32                  "");
33  
34          this.assertVelocity();
35      }
36  
37      public void testPublicFieldGet() throws Exception {
38          this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
39  
40          this.getContext().put("ctx", this.getContext());
41          this.getContext().put("fields", new Fields());
42  
43          this.setTemplate("" +
44                  "$fields.field\n" +
45                  "");
46  
47          this.setExpected("" +
48                  "public field\n" +
49                  "");
50  
51          this.assertVelocity();
52      }
53  
54      public void testPublicStaticFieldGet() throws Exception {
55          this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
56  
57          this.getContext().put("ctx", this.getContext());
58          this.getContext().put("fields", new Fields());
59  
60          this.setTemplate("" +
61                  "$fields.staticField\n" +
62                  "");
63  
64          this.setExpected("" +
65                  "public static field\n" +
66                  "");
67  
68          this.assertVelocity();
69      }
70  
71      public void testPublicFieldGetInt() throws Exception {
72          this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
73  
74          this.getContext().put("ctx", this.getContext());
75          this.getContext().put("fields", new Fields());
76  
77          this.setTemplate("" +
78                  "$fields.intField\n" +
79                  "");
80  
81          this.setExpected("" +
82                  "123\n" +
83                  "");
84  
85          this.assertVelocity();
86      }
87  
88      public void testPublicFieldDontGetProtected() throws Exception {
89          this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
90  
91          this.getContext().put("ctx", this.getContext());
92          this.getContext().put("fields", new Fields());
93  
94          this.setTemplate("" +
95                  "$fields.protectedField\n" +
96                  "");
97  
98          this.setExpected("" +
99                  "$fields.protectedField\n" +
100                 "");
101 
102         this.assertVelocity();
103     }
104 
105     public void testPublicFieldGetNotFound() throws Exception {
106         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
107 
108         this.getContext().put("ctx", this.getContext());
109         this.getContext().put("fields", new Fields());
110 
111         this.setTemplate("" +
112                 "$fields.fiel\n" +
113                 "");
114 
115         this.setExpected("" +
116                 "$fields.fiel\n" +
117                 "");
118 
119         this.assertVelocity();
120     }
121 
122     public void testNormalSet() throws Exception {
123         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
124 
125         this.getContext().put("ctx", this.getContext());
126         Fields fields = new Fields();
127         this.getContext().put("fields", fields);
128 
129         this.setTemplate("" +
130                 "#set($fields.theField = 'set!')\n" +
131                 "");
132 
133         this.setExpected("" +
134                 "");
135 
136         this.assertVelocity();
137         assertEquals("set!", fields.getTheField());
138     }
139 
140     public void testPublicFieldSet() throws Exception {
141         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
142 
143         this.getContext().put("ctx", this.getContext());
144         Fields fields = new Fields();
145         this.getContext().put("fields", fields);
146 
147         this.setTemplate("" +
148                 "#set($fields.field = 'set!')\n" +
149                 "");
150 
151         this.setExpected("" +
152                 "");
153 
154         this.assertVelocity();
155         assertEquals("set!", fields.getTheField());
156     }
157 
158     public void testPublicFieldSetInt() throws Exception {
159         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
160 
161         this.getContext().put("ctx", this.getContext());
162         Fields fields = new Fields();
163         this.getContext().put("fields", fields);
164 
165         this.setTemplate("" +
166                 "#set($fields.intField = 100)\n" +
167                 "");
168 
169         this.setExpected("" +
170                 "");
171 
172         this.assertVelocity();
173         assertEquals(100, fields.intField);
174     }
175 
176     public void testPublicFieldDontSetProtected() throws Exception {
177         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
178 
179         this.getContext().put("ctx", this.getContext());
180         Fields fields = new Fields();
181         this.getContext().put("fields", fields);
182 
183         this.setTemplate("" +
184                 "#set($fields.protectedField = 'set!')\n" +
185                 "");
186 
187         this.setExpected("" +
188                 "");
189 
190         this.assertVelocity();
191         assertEquals("protected field", fields.protectedField);
192     }
193 
194     public void testPublicFieldSetNotFound() throws Exception {
195         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
196 
197         this.getContext().put("ctx", this.getContext());
198         this.getContext().put("fields", new Fields());
199 
200         this.setTemplate("" +
201                 "#set($fields.fiel = 'set!')\n" +
202                 "");
203 
204         this.setExpected("" +
205                 "");
206 
207         this.assertVelocity();
208     }
209 
210     public void testIterator() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
211         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
212 
213         this.getContext().put("ctx", this.getContext());
214         this.getContext().put("fields", new Fields());
215 
216         this.setTemplate("" +
217                 "#foreach($element in $fields.booleanArray)\n" +
218                 "$element\n" +
219                 "#end\n" +
220                 "");
221 
222         this.setExpected("" +
223                 "true\n" +
224                 "false\n" +
225                 "");
226 
227         this.assertVelocity();
228     }
229 
230     public void testBooleanArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
231         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
232 
233         this.getContext().put("ctx", this.getContext());
234         this.getContext().put("fields", new Fields());
235 
236         this.setTemplate("" +
237                 "$fields.booleanArray.length\n" +
238                 "");
239 
240         this.setExpected("" +
241                 "2\n" +
242                 "");
243 
244         this.assertVelocity();
245     }
246 
247     public void testCharArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
248         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
249 
250         this.getContext().put("ctx", this.getContext());
251         this.getContext().put("fields", new Fields());
252 
253         this.setTemplate("" +
254                 "$fields.charArray.length\n" +
255                 "");
256 
257         this.setExpected("" +
258                 "1\n" +
259                 "");
260 
261         this.assertVelocity();
262     }
263 
264     public void testByteArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
265         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
266 
267         this.getContext().put("ctx", this.getContext());
268         this.getContext().put("fields", new Fields());
269 
270         this.setTemplate("" +
271                 "$fields.byteArray.length\n" +
272                 "");
273 
274         this.setExpected("" +
275                 "3\n" +
276                 "");
277 
278         this.assertVelocity();
279     }
280 
281     public void testShortArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
282         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
283 
284         this.getContext().put("ctx", this.getContext());
285         this.getContext().put("fields", new Fields());
286 
287         this.setTemplate("" +
288                 "$fields.shortArray.length\n" +
289                 "");
290 
291         this.setExpected("" +
292                 "4\n" +
293                 "");
294 
295         this.assertVelocity();
296     }
297 
298     public void testIntArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
299         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
300 
301         this.getContext().put("ctx", this.getContext());
302         this.getContext().put("fields", new Fields());
303 
304         this.setTemplate("" +
305                 "$fields.intArray.length\n" +
306                 "");
307 
308         this.setExpected("" +
309                 "5\n" +
310                 "");
311 
312         this.assertVelocity();
313     }
314 
315     public void testLongArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
316         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
317 
318         this.getContext().put("ctx", this.getContext());
319         this.getContext().put("fields", new Fields());
320 
321         this.setTemplate("" +
322                 "$fields.longArray.length\n" +
323                 "");
324 
325         this.setExpected("" +
326                 "6\n" +
327                 "");
328 
329         this.assertVelocity();
330     }
331 
332     public void testFloatArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
333         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
334 
335         this.getContext().put("ctx", this.getContext());
336         this.getContext().put("fields", new Fields());
337 
338         this.setTemplate("" +
339                 "$fields.floatArray.length\n" +
340                 "");
341 
342         this.setExpected("" +
343                 "7\n" +
344                 "");
345 
346         this.assertVelocity();
347     }
348 
349     public void testDoubleArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
350         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
351 
352         this.getContext().put("ctx", this.getContext());
353         this.getContext().put("fields", new Fields());
354 
355         this.setTemplate("" +
356                 "$fields.doubleArray.length\n" +
357                 "");
358 
359         this.setExpected("" +
360                 "8\n" +
361                 "");
362 
363         this.assertVelocity();
364     }
365 
366     public void testObjectArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
367         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
368 
369         this.getContext().put("ctx", this.getContext());
370         this.getContext().put("fields", new Fields());
371 
372         this.setTemplate("" +
373                 "$fields.objectArray.length\n" +
374                 "");
375 
376         this.setExpected("" +
377                 "9\n" +
378                 "");
379 
380         this.assertVelocity();
381     }
382 
383     public void testStringArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
384         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
385 
386         this.getContext().put("ctx", this.getContext());
387         this.getContext().put("fields", new Fields());
388 
389         this.setTemplate("" +
390                 "$fields.stringArray.length\n" +
391                 "");
392 
393         this.setExpected("" +
394                 "10\n" +
395                 "");
396 
397         this.assertVelocity();
398     }
399 
400     public void testNotArrayLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
401         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
402 
403         this.getContext().put("ctx", this.getContext());
404         this.getContext().put("fields", new Fields());
405 
406         this.setTemplate("" +
407                 "$fields.field.length\n" +
408                 "");
409 
410         this.setExpected("" +
411                 "$fields.field.length\n" +
412                 "");
413 
414         this.assertVelocity();
415     }
416 
417     public void testNullLength() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
418         this.getEngine().setProperty("runtime.introspector.uberspect", "org.apache.velocity.tools.generic.introspection.PublicFieldUberspect");
419 
420         this.getContext().put("ctx", this.getContext());
421         this.getContext().put("fields", new Fields());
422 
423         this.setTemplate("" +
424                 "$fields.nullArray.length\n" +
425                 "");
426 
427         this.setExpected("" +
428                 "$fields.nullArray.length\n" +
429                 "");
430 
431         this.assertVelocity();
432     }
433 
434     public void testArrayInstance() {
435         Object booleanArray = new boolean[0];
436         Object byteArray = new byte[0];
437         Object shortArray = new short[0];
438         Object intArray = new int[0];
439         Object longArray = new long[0];
440         Object floatArray = new float[0];
441         Object doubleArray = new double[0];
442         Object objectArray = new Object[0];
443         Object stringArray = new String[0];
444 
445         assertTrue(booleanArray instanceof boolean[]);
446         assertTrue(byteArray instanceof byte[]);
447         assertFalse(byteArray instanceof short[]);
448         assertTrue(shortArray instanceof short[]);
449         assertFalse(shortArray instanceof byte[]);
450         assertTrue(intArray instanceof int[]);
451         assertTrue(longArray instanceof long[]);
452         assertTrue(floatArray instanceof float[]);
453         assertTrue(doubleArray instanceof double[]);
454         assertTrue(objectArray instanceof Object[]);
455         assertTrue(stringArray instanceof Object[]);
456     }
457 
458     public void testArrayFields() {
459         int[] array = new int[3];
460         Class clazz = array.getClass();
461 
462         Field[] fields = clazz.getFields();
463         Field[] declaredFields = clazz.getDeclaredFields();
464 
465         assertEquals(0, fields.length);
466         assertEquals(0, declaredFields.length);
467     }
468 
469     public void testArrayMethods() {
470         int[] array = new int[3];
471         Class clazz = array.getClass();
472 
473         Method[] methods = clazz.getMethods();
474         Method[] declaredMethods = clazz.getDeclaredMethods();
475 
476         this.getLog().warn("Methods:"+methods.length);
477         for (int index = 0; index < methods.length; ++index) {
478             this.getLog().warn("  Method["+index+"]:"+methods[index]);
479         }
480         assertEquals(0, declaredMethods.length);
481     }
482 
483     public static class Fields {
484         public String field = "public field";
485         public static String staticField = "public static field";
486         protected String protectedField = "protected field";
487         public int intField = 123;
488         public boolean[] booleanArray = new boolean[] {true, false};
489         public char[] charArray = new char[] {'1'};
490         public byte[] byteArray = new byte[] {1, 2, 3};
491         public short[] shortArray = new short[] {1, 2, 3, 4};
492         public int[] intArray = new int[] {1, 2, 3, 4, 5};
493         public long[] longArray = new long[] {1, 2, 3, 4, 5, 6};
494         public float[] floatArray = new float[] {1, 2, 3, 4, 5, 6, 7};
495         public double[] doubleArray = new double[] {1, 2, 3, 4, 5, 6, 7, 8};
496         public Object[] objectArray = new Object[] {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
497         public String[] stringArray = new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
498         public Object[] nullArray = null;
499 
500         public String getTheField() {
501             return this.field;
502         }
503         public void setTheField(String field) {
504             this.field = field;
505         }
506     }
507 
508 }